package cn.itcast.service.system.impl;

import cn.itcast.dao.system.RoleDao;
import cn.itcast.domain.system.Role;
import cn.itcast.service.system.RoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class RoleServiceImpl implements RoleService {

 @Autowired
 private RoleDao roleDao;

 //分页查询
 public PageInfo findAll(String companyId, int page, int size) {
  //设置分页
  PageHelper.startPage(page, size);
  //调用dao查询
  List<Role> list = roleDao.findAll(companyId);
  return new PageInfo(list);
 }

 //保存
 public void save(Role role) {
  //设置id
  role.setId(UUID.randomUUID().toString());
  //调用dao
  roleDao.save(role);
 }

 //更新
 public void update(Role role) {
  roleDao.update(role);
 }

 //根据id查询
 public Role findById(String id) {
  return roleDao.findById(id);
 }

 //根据id删除
 public void delete(String id) {
  roleDao.delete(id);
 }

 //查询所有角色
 public List<Role> findAll(String companyId) {
  return roleDao.findAll(companyId);
 }

 //查询此角色具有的所有模块id
 public List<String> findModulesByRoleId(String roleId) {
  return roleDao.findModulesByRoleId(roleId);
 }

 //实现对角色分配权限
 public void updateRoleModule(String roleId, String moduleIds) {
  //调用dao根据角色id删除角色模块中间表数据
  roleDao.deleteRoleModule(roleId);
  // 将moduleIds拆分成模块id数组
  String[] mid = moduleIds.split(",");
  //循环所有id数组
  for (String moduleId : mid) {
   //调用dao向角色模块中间表保存数据
   roleDao.saveRoleModule(roleId, moduleId);
  }
 }
}
